<template>
	<div class="login_container">
		<!-- 这是登录页面 -->
		<div class="login_box">
			<div class="avatar_box">
				<img src="../assets/logo.png" alt="">
			</div>
			<el-form ref="loginFormRef" :rules="loginFormRules" :model="loginForm" class="login_form" label-width="0px">
				<!-- 用户名 -->
				<el-form-item prop="username">
					<el-input prefix-icon="iconfont icon-user" placeholder="请输入账号" v-model="loginForm.username"></el-input>
				</el-form-item>
				<!-- 密码 -->
				<el-form-item prop="password">
					<el-input prefix-icon="iconfont icon-lock_fill" placeholder="请输入密码" v-model="loginForm.password"></el-input>
				</el-form-item>
				<!-- 按钮 -->
				<el-form-item class="btns">
					<el-button type="primary" round @click="postLoginForm">登录</el-button>
					<el-button type="info" round @click="resetLoginForm">重置</el-button>
				</el-form-item>
			</el-form>
		</div>
	</div>
</template>

<script>
	export default {
		// name: 'vm',
		data () {
			return {
				// 这里写上数据
				loginFormRef: {},
				loginForm: {
					username: 'admin',
					password: '123456'
				},
				loginFormRules: {
					username: [
						{ required: true, message: '请输入账号', trigger: 'blur' },
						{ min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
					],
					password: [
						{ required: true, message: '请输入密码', trigger: 'blur' },
						{ min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
					// 	{ required: true, message: '请输入密码', trigger: 'blur' },
					// 	{ min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
					]
				}
			}
		},
		methods: {
			postLoginForm: function () {
				this.$refs.loginFormRef.validate(async valid => {
					if (!valid) return
					// 在这里写发送登录请求的代码
					// console.log(1)
					const { data: res } = await this.$http.post('login', this.loginForm)
					// console.log(res.data)
					if (res.meta.status !== 200) return this.$message.error('登录失败')
					this.$message.success('登录成功')
					window.sessionStorage.setItem('token', res.data.token)
					this.$router.push('/home')
				})
			},
			resetLoginForm: function () {
				// console.log(this.loginForm)
				this.$refs.loginFormRef.resetFields()
				this.loginForm.username = 'admin'
				this.loginForm.password = '123456'
			}
		}
	}
</script>

<style lang="less" scoped>
	.login_container {
		height: 100%;
		background-color: #2b4b6b;
	}

	.login_box {
		width: 450px;
		height: 300px;
		background-color: #fff;
		border-radius: 5px;
		position: absolute;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -50%);
	}

	.avatar_box {
		height: 130px;
		width: 130px;
		border-radius: 50%;
		box-shadow: 0 0 10px #ddd;
		background-color: #fff;
		padding: 10px;
		position: absolute;
		left: 50%;
		transform: translate(-50%, -50%);

		//top: 50%;
		img {
			height: 100%;
			width: 100%;
			border-radius: 50%;
			background-color: #eee;
		}
	}

	.btns {
		display: flex;
		justify-content: flex-end;
	}

	.login_form {
		position: absolute;
		bottom: 0;
		width: 100%;
		padding: 0 20px;
		box-sizing: border-box;
	}
</style>
